
#include "CDSP/signals.h"
#include "CDSP/config.h"
#ifdef WINDOWS_PC_MAIN 
#include "waveHelper.h"
#include <stdio.h>
#include <stdlib.h>


short output[13]={0};
short input[] = {0,-10,-13,20,13,13,5,0,-6};
short imres[] = {10,-4,-3,-2};

double echoBuff[] = {0,0,2,0,0,0,0,0,0};




char* debugBuffer;
int debugBufferDataLength=0;

void addToBuffer(short c)
{
	int len=sprintf(debugBuffer+debugBufferDataLength,"%d",c);
	debugBufferDataLength+=len;
}

void displaySignal(SignalStr* sig,int height)
{
	int j;
	for(j=0;j<sig->length;j++)
	{
		addToBuffer(sig->signal[j]);
		debugBuffer[debugBufferDataLength++]=',';
	}
	debugBuffer[debugBufferDataLength++]='\n';
}


int main(int argc,char*argv[])
{
	FILE* f,*f2;
	short* buffer;
	short* buffer2;
	SignalStr sig,im,out,echo;
	int len;
	int read;

	//init code
	debugBuffer =(char*) malloc(1024*2);

	//init the signals
	sig.signal=input;
	sig.length=sizeof(input)/sizeof(short);

	im.signal=imres;
	im.length=sizeof(imres)/sizeof(short);
	out.signal=output;
	out.length=im.length+sig.length;

	//the math
	convolution(&im,&sig,&out);
	
	
	//debuging
//	displaySignal(&sig,30);
//	displaySignal(&im,30);
//	displaySignal(&out,30);

//	f = fopen("out.csv","w");
//	if(f)
//	{
//	fwrite(debugBuffer,sizeof(unsigned short),debugBufferDataLength,f);
//	fclose(f);
//	}
//	else
	{
//		printf("close the excel");
	}
//	debugBuffer[debugBufferDataLength]=0;
//	printf(debugBuffer);

	

	buffer=0;
	buffer = (short*)malloc(1024*2000*2);
	buffer2=0;
	buffer2 = (short*)malloc(1024*2000*2);
	f2=0;
	f2 = fopen("..\\TestData\\guit.raw","rb");
	len=0;
	do
	{
		size_t lenlen = 1024*2000-len;
		read = fread(buffer+len,sizeof(short),lenlen,f2);
		len+=read;
	}while(read>0);
	fclose(f2);

	im.signal=buffer;
	im.length=len;
	out.signal=buffer2;
	out.length=1024*2000;
	//try it out on some audio
	echo.signal=echoBuff;
	echo.length=sizeof(echoBuff)/sizeof(short);
	convolution(&im,&echo,&out);
	/*
	for(read=0;read<out.length;read++)
	{
		out.signal[read]=out.signal[read]/10;
	}
	*/
	writeWaveFile(buffer2,len,"test.wav");
	getchar();
	return 0;
}
#endif